﻿Imports System.Text.RegularExpressions
Imports Kirishima16.Net.Twitter
Imports System.Runtime.Serialization.Json
Imports System.IO
Imports System.Text

Namespace Anis.Net.Twitter
    Partial Class TwitterClient
#Region "TwitterUser関連"
        ''' <summary>
        ''' APIの応答結果からTwitterUser()を生成します
        ''' </summary>
        ''' <param name="jsonStr">APIが返すXML</param>
        Private Function CreateTwitterUsers(ByVal jsonStr As String) As TwitterUser()
            jsonStr = _createdAtRegex.Replace(jsonStr, New MatchEvaluator(AddressOf DateMatchEvaluator))
            jsonStr = _idNullRegex.Replace(jsonStr, "0")
            jsonStr = _followingNullRegex.Replace(jsonStr, "false")

            Dim serializer As New DataContractJsonSerializer(GetType(TwitterUser()))
            Using stream As New MemoryStream(Encoding.UTF8.GetBytes(jsonStr))
                Return DirectCast(serializer.ReadObject(stream), TwitterUser())
            End Using

        End Function

        ''' <summary>
        ''' APIの応答結果からTwitterUserを生成します
        ''' </summary>
        ''' <param name="jsonStr">APIが返すJSON</param>
        Private Function CreateTwitterUser(ByVal jsonStr As String) As TwitterUser
            jsonStr = _createdAtRegex.Replace(jsonStr, New MatchEvaluator(AddressOf DateMatchEvaluator))
            jsonStr = _idNullRegex.Replace(jsonStr, "0")
            jsonStr = _followingNullRegex.Replace(jsonStr, "false")

            Dim serializer As New DataContractJsonSerializer(GetType(TwitterUser))
            Using stream As New MemoryStream(Encoding.UTF8.GetBytes(jsonStr))
                Return DirectCast(serializer.ReadObject(stream), TwitterUser)
            End Using
        End Function

#End Region

#Region "TwitterStatus関連"
        ''' <summary>
        ''' APIの応答結果からTwitterStatus()を生成します
        ''' </summary>
        ''' <param name="jsonStr">APIが返すJSON</param>
        Private Function CreateTwitterStatuses(ByVal jsonStr As String) As TwitterStatus()
            jsonStr = _createdAtRegex.Replace(jsonStr, New MatchEvaluator(AddressOf DateMatchEvaluator))
            jsonStr = _idNullRegex.Replace(jsonStr, "0")
            jsonStr = _followingNullRegex.Replace(jsonStr, "false")

            Dim serializer As New DataContractJsonSerializer(GetType(TwitterStatus()))
            Using stream As New MemoryStream(Encoding.UTF8.GetBytes(jsonStr))
                Return DirectCast(serializer.ReadObject(stream), TwitterStatus())
            End Using

            'これでも動くけどどっちがいいんだろう
            'Dim ser As New System.ServiceModel.Dispatcher.JsonQueryStringConverter()
            'Return DirectCast(ser.ConvertStringToValue(jsonStr, GetType(TwitterStatus())), TwitterStatus())
        End Function

        ''' <summary>
        ''' APIの応答結果からTwitterStatusを生成します
        ''' </summary>
        ''' <param name="jsonStr">APIが返すJSON</param>
        Private Function CreateTwitterStatus(ByVal jsonStr As String) As TwitterStatus
            jsonStr = _createdAtRegex.Replace(jsonStr, New MatchEvaluator(AddressOf DateMatchEvaluator))
            jsonStr = _idNullRegex.Replace(jsonStr, "0")
            jsonStr = _followingNullRegex.Replace(jsonStr, "false")

            Dim serializer As New DataContractJsonSerializer(GetType(TwitterStatus))
            Using stream As New MemoryStream(Encoding.UTF8.GetBytes(jsonStr))
                Return DirectCast(serializer.ReadObject(stream), TwitterStatus)
            End Using
        End Function
#End Region

#Region "TwitterClient.Timeline.vbからコピペ"
        Private _createdAtRegex As New Regex("(?<=""created_at""\:"")(Sun|Mon|Tue|Wed|Thu|Fri|Sat) (?<mon>Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) (?<day>\d{2}) (?<hour>\d{2})\:(?<min>\d{2})\:(?<sec>\d{2}) \+\d{4} (?<year>\d{4})")
        Private _idNullRegex As New Regex("(?<=id""\:)null")
        Private _followingNullRegex As New Regex("(?<=following""\:)null")
        Private _dtUnixBegin As New DateTime(1970, 1, 1)
        Private Function DateMatchEvaluator(ByVal m As Match) As String
            Dim year As Integer = Integer.Parse(m.Groups("year").Value)
            Dim mon As Integer
            Select Case m.Groups("mon").Value
                Case "Jan"
                    mon = 1
                Case "Feb"
                    mon = 2
                Case "Mar"
                    mon = 3
                Case "Apr"
                    mon = 4
                Case "May"
                    mon = 5
                Case "Jun"
                    mon = 6
                Case "Jul"
                    mon = 7
                Case "Aug"
                    mon = 8
                Case "Sep"
                    mon = 9
                Case "Oct"
                    mon = 10
                Case "Nov"
                    mon = 11
                Case "Dec"
                    mon = 12
            End Select
            Dim day As Integer = Integer.Parse(m.Groups("day").Value)
            Dim hour As Integer = Integer.Parse(m.Groups("hour").Value)
            Dim min As Integer = Integer.Parse(m.Groups("min").Value)
            Dim sec As Integer = Integer.Parse(m.Groups("sec").Value)
            Dim dt = New DateTime(year, mon, day, hour, min, sec)
            Dim ret = String.Format("\/Date({0}+0900)\/", dt.Subtract(_dtUnixBegin).TotalMilliseconds)
            Return ret
        End Function
#End Region
    End Class
End Namespace